#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class StaffExprienceProvider : CY.GFive.Core.Providers.DALProvider.IStaffExprienceProvider
	{
		#region Sql
		private static readonly string SqlSelectStaffExprience = "SELECT [Id], [Code],[Positon],[Title] FROM [StaffExprience] WHERE  [Id]=@Id";

		private static readonly string SqlInsertStaffExprience = "INSERT INTO [StaffExprience]([Code],[Positon],[Title]) values(@Code,@Positon,@Title);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateStaffExprience = "UPDATE [StaffExprience] SET [Code] = @Code,[Positon] = @Positon,[Title] = @Title WHERE  [Id]=@Id";

		private static readonly string SqlDeleteStaffExprience = "DELETE [StaffExprience] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllStaffExprience = "SELECT [Id], [Code],[Positon],[Title] FROM [StaffExprience]";
		#endregion

		#region IStaffExprienceProvider Members
		public Core.Business.StaffExprience Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectStaffExprience);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.StaffExprience staffExprience = new Core.Business.StaffExprience();

				if (!reader.IsDBNull(0)) staffExprience.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) staffExprience.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) staffExprience.Positon = reader.GetString(2);
				if (!reader.IsDBNull(3)) staffExprience.Title = reader.GetString(3);

				return staffExprience;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.StaffExprience staffExprience)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, staffExprience.Code);
			sql.AddParameter("@Positon", SqlDbType.NVarChar, staffExprience.Positon);
			sql.AddParameter("@Title", SqlDbType.NVarChar, staffExprience.Title);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertStaffExprience);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) staffExprience.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.StaffExprience staffExprience)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, staffExprience.Code);
			sql.AddParameter("@Positon", SqlDbType.NVarChar, staffExprience.Positon);
			sql.AddParameter("@Title", SqlDbType.NVarChar, staffExprience.Title);
			sql.AddParameter("@Id", SqlDbType.Int, staffExprience.Id);
			sql.ExecuteSql(SqlUpdateStaffExprience);
		}

		public void Delete(Core.Business.StaffExprience staffExprience)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, staffExprience.Id);
			sql.ExecuteSql(SqlDeleteStaffExprience);
		}

		public IList<Core.Business.StaffExprience> GetAllStaffExprience()
		{
			IList<Core.Business.StaffExprience> staffExpriencelist = new List<Core.Business.StaffExprience>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllStaffExprience);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.StaffExprience staffExprience = new Core.Business.StaffExprience();

					if (!reader.IsDBNull(0)) staffExprience.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) staffExprience.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) staffExprience.Positon = reader.GetString(2);
					if (!reader.IsDBNull(3)) staffExprience.Title = reader.GetString(3);

					staffExprience.MarkOld();
					staffExpriencelist.Add(staffExprience);
				}
				reader.Close();
			}
			return staffExpriencelist;
		}
		#endregion
	}
}
